<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="外出地点" prop="outSite">
        <el-input v-model="dataForm.outSite" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="外出事由" prop="outReason">
        <el-input v-model="dataForm.outReason" placeholder=""></el-input>
      </el-form-item>

      <el-form-item label="外出时长" prop="outHour">
        <el-input v-model="dataForm.outHour" placeholder=""></el-input>
      </el-form-item>

      <el-form-item label="外出时间段" prop="outHour">
        <!-- <el-input v-model="dataForm.outHour" placeholder=""></el-input> -->
        <el-date-picker
          v-model="dataForm.combineTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        userId: '',
        outSite: '',
        outReason: '',
        status: '',
        outHour: '',
        outTime: '',
        returnTime: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        userId: [{ required: true, message: '不能为空', trigger: 'blur' }],
        outSite: [{ required: true, message: '不能为空', trigger: 'blur' }],
        outReason: [{ required: true, message: '不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '不能为空', trigger: 'blur' }],
        outHour: [{ required: true, message: '不能为空', trigger: 'blur' }],
        outTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        returnTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        createTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/outapplyrecord/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.outApplyRecord.userId
              this.dataForm.outSite = data.outApplyRecord.outSite
              this.dataForm.outReason = data.outApplyRecord.outReason
              this.dataForm.status = data.outApplyRecord.status
              this.dataForm.outHour = data.outApplyRecord.outHour
              this.dataForm.outTime = data.outApplyRecord.outTime
              this.dataForm.returnTime = data.outApplyRecord.returnTime
              this.dataForm.createTime = data.outApplyRecord.createTime
              this.dataForm.updateTime = data.outApplyRecord.updateTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/outapplyrecord/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              userId: this.dataForm.userId,
              outSite: this.dataForm.outSite,
              outReason: this.dataForm.outReason,
              status: this.dataForm.status,
              outHour: this.dataForm.outHour,
              outTime: this.dataForm.outTime,
              returnTime: this.dataForm.returnTime,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
